package com.ptmall.app.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.ptmall.app.R;
import com.ptmall.app.alipay.AliPay;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.RechargeWalletBean;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.wxapi.PayResultListener;
import com.xfly.app.wxapi.WxPay;

import java.util.HashMap;
import java.util.Map;

public class RechargeWalletActivity extends BaseMvpActivity {

    TitleBarView titlebar;
    CheckBox ali_check,wx_check;
    EditText rechargenum;
    Button enter;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_recharge);
    }

    @Override
    public void initViews() {
        titlebar = (TitleBarView) findViewById(R.id.titlebar);
        ali_check = (CheckBox) findViewById(R.id.ali_check);
        wx_check = (CheckBox) findViewById(R.id.wx_check);
        rechargenum = (EditText) findViewById(R.id.rechargenum);
        enter = (Button) findViewById(R.id.enter);
        titlebar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(wx_check.isChecked()){
                    putRecharge("2",rechargenum.getText().toString());
                }
                if(ali_check.isChecked()){
                    putRecharge("1",rechargenum.getText().toString());
                }
            }
        });

        wx_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    ali_check.setChecked(false);
                }
            }
        });

        ali_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    wx_check.setChecked(false);
                }
            }
        });

    }

    @Override
    public void initEvents() {

    }

    private void putRecharge(String pay_method,String amount){
        Map<String,String> map = new HashMap<>();
        map.put("pay_method",pay_method);
        map.put("amount",amount);
        apiDataRepository.putRechargeWallet(map, new ApiNetResponse<RechargeWalletBean>(getContext()) {
            @Override
            public void onSuccess(RechargeWalletBean data) {
//                showMsg("获取充值凭证成功");
                if(data.pay_method == 1){
                    AliPay.createInstance(RechargeWalletActivity.this, mPayResultListener).pay(data.alipay);
                }else if(data.pay_method == 2){
                    WxPay.getInstance(RechargeWalletActivity.this).pay(data.weixin, mPayResultListener);
                }
            }
        });
    }


    PayResultListener mPayResultListener = new PayResultListener() {
        @Override
        public void onSuccess(String info) {
            //后台确认支付结果
            showMsg("支付成功");
            finish();
        }

        @Override
        public void onFail(String info) {
            if(info!=null&&info.equals("{6001}")){
                showMsg("取消支付");
                return;
            }
            showMsg(info);
        }
    };
}
